/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.colegiatura.beans;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import org.cip.esymons.colegiatura.be.MediaData;
import org.cip.esymons.colegiatura.be.codeGenerator;

/**
 *
 * @author WILLIAM GASTAÑUEDI CABALLERO
 */
public class CodeGeneratorController {

   private String CodigoGenerado;


//METODO QUE PERMITE CREAR Y VISUALIZAR UN CODIGO AUTOGENERADO EN FORMATO PNG.
   public void paint(OutputStream out, Object data) throws IOException{
//        Integer high = 9999;
//        Integer low = 1000;
//        Random generator = new Random();
//        Integer digits = generator.nextInt(high - low + 1) + low;

        String digits = codeGenerator.getPassword(
		codeGenerator.MAYUSCULAS+
		codeGenerator.NUMEROS,6);
        CodigoGenerado = "";
        setCodigoGenerado(digits);
//        CodigoGenerado = digits;

        if (data instanceof MediaData) {
            MediaData paintData = (MediaData) data;
            BufferedImage img = new BufferedImage(paintData.getWidth(),paintData.getHeight(),BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D = img.createGraphics();
            graphics2D.setBackground(paintData.getBackground());
            graphics2D.setColor(paintData.getDrawColor());
            graphics2D.clearRect(0,0,paintData.getWidth(),paintData.getHeight());
            graphics2D.setFont(paintData.getFont());
            graphics2D.drawString(digits.toString(), 20, 35);
            ImageIO.write(img,"png",out);
        }
    }

    public String getCodigoGenerado() {
        return CodigoGenerado;
    }

    public void setCodigoGenerado(String CodigoGenerado) {
        this.CodigoGenerado = CodigoGenerado;
    }

}
